package netWorkPrograming.test.test02;

import java.io.*;
import java.net.Socket;

/**
 * @author yangwenqi
 * @decs TODO
 * @date 2025/5/28
 */
public class ClientDemo {
    public static void main(String[] args) {
        File file=new File("C:\\Users\\23838\\Desktop\\004.png");
        Socket socket=null;
        BufferedInputStream bis=null;
        PrintStream ps=null;
        try {
            socket=new Socket("127.0.0.1",8888);
            //先把硬盘中的文件读到内存中
            bis=new BufferedInputStream(new FileInputStream(file));
            //再把内存中的文件写到socket
            ps=new PrintStream(socket.getOutputStream());
            //边读边写
            byte[] buf=new byte[1024];
            int len;
            while((len=bis.read(buf))!=-1){
                ps.write(buf,0,len);
            }
            System.out.println("客户端上传成功");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally {
            if(bis!=null){
                try {
                    bis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if(ps!=null){
                ps.close();
            }
            if(socket!=null){
                try {
                    socket.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
